/********** 有理数的运算 **********/
/********** 数据是什么 **********/
function makeRat(n, d) {
  return function (i) {
    if (i === 0) return n;
    if (i === 1) return d;
    return new Error('must be 1 or 0');
  };
}
function numer(x) {
  return x(0);
}
function denom(x) {
  return x(1);
}
function printRat(x) {
  console.log(`${numer(x)}/${denom(x)}`);
}

function addRat(x, y) {
  return makeRat(
    numer(x) * denom(y) + numer(y) * denom(x),
    denom(x) * denom(y)
  );
}
function subRat(x, y) {
  return makeRat(
    numer(x) * denom(y) - numer(y) * denom(x),
    denom(x) * denom(y)
  );
}
function mulRat(x, y) {
  return makeRat(numer(x) * numer(y), denom(x) * denom(y));
}
function divRat(x, y) {
  return makeRat(numer(x) * denom(y), denom(x) * numer(y));
}
function equalRat(x, y) {
  return numer(x) * denom(y) === denom(x) * numer(y);
}

const oneHalf = makeRat(1, 2);
printRat(oneHalf);
const oneThird = makeRat(1, 3);
printRat(oneThird);
printRat(addRat(oneHalf, oneThird));
printRat(mulRat(oneHalf, oneThird));
printRat(addRat(oneThird, oneThird));

/********** 抽象屏障 **********/
function makeSegment(startPoint, endPoint) {
  return [startPoint, endPoint];
}
function startSegment(segment) {
  return segment[0];
}
function endSegment(segment) {
  return segment[1];
}
function makePoint(x, y) {
  return [x, y];
}
function xPoint(point) {
  return point[0];
}
function yPoint(point) {
  return point[1];
}

function pair(x, y) {
  return m => m(x, y);
}
function head(z) {}
function tail(z) {}
console.log(head(pair(3, 4)));
